package cz.muni.fi.pa165.library.web_layer.adminPages.Reader;

import cz.muni.fi.pa165.library.web_layer.adminPages.reservation.EditReservation;
import cz.muni.fi.pa165.library.web_layer.adminPages.Reader.EditReader;
import cz.muni.fi.pa165.library.api.ReaderService;
import cz.muni.fi.pa165.library.api.ReaderTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.HomePage;
import cz.muni.fi.pa165.library.web_layer.Template;
import cz.muni.fi.pa165.library.web_layer.adminPages.reservation.EditReservation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author Matúš Abaffy
 */
public class ShowAllReader extends Template {

    private static final long serialVersionUID = 1L;

    public ShowAllReader(final PageParameters parameters) {
        super(parameters);

        RepeatingView repeating = new RepeatingView("repeating");
        add(repeating);

        ReaderService readerService = (ReaderService) ApplicationContextProvider.getApplicationContext().getBean("readerService");
        for (ReaderTO reader : readerService.findAllReaders()) {

            AbstractItem item = new AbstractItem(repeating.newChildId());

            PageParameters pageParameters = new PageParameters();
            pageParameters.add("readerId", reader.getId());
            item.add(new BookmarkablePageLink<Void>("edit", EditReader.class, pageParameters));
            item.add(new BookmarkablePageLink<Void>("delete", ShowAllReader.class, pageParameters));
            item.add(new Label("firstName", reader.getFirstName()));
            item.add(new Label("surname", reader.getSurname()));
            item.add(new Label("birthNumber", reader.getBirthNumber()));
            item.add(new Label("address", reader.getAddress()));
            item.add(new Label("email", reader.getEmail()));
            item.add(new Label("telephoneNumber", reader.getTelephoneNumber()));
            
            item.add(new BookmarkablePageLink<Void>("reserve", EditReservation.class, pageParameters));
            repeating.add(item);
        }
        
        StringValue readerId = parameters.get("readerId");
        if (!readerId.isEmpty()) {
            readerService.deleteReader(readerService.findReaderById(readerId.toLong()));
            setResponsePage(ShowAllReader.class);
        }

        BookmarkablePageLink addReaderLink = new BookmarkablePageLink<>(
                "addReader", EditReader.class);
        add(addReaderLink);

        BookmarkablePageLink homeLink = new BookmarkablePageLink<>(
                "home", HomePage.class);
        add(homeLink);
    }
}